package com.ruoyi.broker.service.impl;

import com.ruoyi.broker.domain.Broker;
import com.ruoyi.broker.domain.BrokerApply;
import com.ruoyi.broker.mapper.BrokerApplyMapper;
import com.ruoyi.broker.mapper.BrokerMapper;
import com.ruoyi.broker.service.IBrokerApplyService;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 经纪人审核Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-09
 */
@Service
public class BrokerApplyServiceImpl implements IBrokerApplyService {
    @Autowired
    private BrokerApplyMapper brokerApplyMapper;
    @Resource
    private BrokerMapper brokerMapper;

    /**
     * 查询经纪人审核
     *
     * @param id 经纪人审核主键
     * @return 经纪人审核
     */
    @Override
    public BrokerApply selectBrokerApplyById(Long id) {
        return brokerApplyMapper.selectBrokerApplyById(id);
    }

    /**
     * 查询经纪人审核列表
     *
     * @param brokerApply 经纪人审核
     * @return 经纪人审核
     */
    @Override
    public List<BrokerApply> selectBrokerApplyList(BrokerApply brokerApply) {
        return brokerApplyMapper.selectBrokerApplyList(brokerApply);
    }

    /**
     * 新增经纪人审核
     *
     * @param brokerApply 经纪人审核
     * @return 结果
     */
    @Override
    public int insertBrokerApply(BrokerApply brokerApply) {
        brokerApply.setCreateTime(DateUtils.getNowDate());
        return brokerApplyMapper.insertBrokerApply(brokerApply);
    }

    /**
     * 修改经纪人审核
     *
     * @param brokerApply 经纪人审核
     * @return 结果
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public int updateBrokerApply(BrokerApply brokerApply) {
        if (brokerApply.getAuditStatus() == 1) {
            String selectPartnerTechnicianByTel = brokerApplyMapper.selectPartnerTechnicianByTel(brokerApply.getPartnerTel());
            Integer updated = brokerApplyMapper.updateTechnicianIdentity(brokerApply.getTid(), 1, selectPartnerTechnicianByTel);
            if (updated == 0) {
                throw new RuntimeException("审核失败");
            }
            String invitetionCode = brokerApplyMapper.selectTechnicianInvitetionCode(brokerApply.getTid());

            Broker broker = new Broker();
            broker.setTid(brokerApply.getTid());
            broker.setBrokerName(brokerApply.getName());
            broker.setBrokerTel(brokerApply.getTel());
            broker.setChannelType(brokerApply.getChannelName());
            broker.setBrokerInvitationCode(invitetionCode);
            broker.setBrokerInvitedCode(selectPartnerTechnicianByTel);
            brokerMapper.insertBroker(broker);
        }
        brokerApply.setUpdateTime(DateUtils.getNowDate());
        return brokerApplyMapper.updateBrokerApply(brokerApply);
    }

    /**
     * 批量删除经纪人审核
     *
     * @param ids 需要删除的经纪人审核主键
     * @return 结果
     */
    @Override
    public int deleteBrokerApplyByIds(Long[] ids) {
        return brokerApplyMapper.deleteBrokerApplyByIds(ids);
    }

    /**
     * 删除经纪人审核信息
     *
     * @param id 经纪人审核主键
     * @return 结果
     */
    @Override
    public int deleteBrokerApplyById(Long id) {
        return brokerApplyMapper.deleteBrokerApplyById(id);
    }
}
